#! /usr/bin/python
# Copyright (c) 2008 by University College London
# Authors:
# Didrik Pinte <dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.
# Lastest modif by : $Author: dpinte $
# Version : $Revision: 489 $
# File : $HeadURL: https://secure2.svnrepository.com/s_dpinte/hydroplatform/src/python/trunk/hydroplatform.py $
# Date : $Date: 2011-05-23 14:19:44 +0100 (Mon, 23 May 2011) $
# Id: $Id $
__revision__ = '$Revision: 489 $'

import logging
import sys, os
import hydroplatform

if not hasattr(sys, 'frozen'):
    import wxversion
    wxversion.select("2.8")


logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# Put the lib dir into the path. The lib dir contains some extra non
# really hydroplatform specific Python modules
hydroplatformdir = os.path.join(hydroplatform.__path__[0], '..')
hptdir = os.path.join(hydroplatformdir, "lib")
if os.path.isdir(hptdir):
    sys.path.insert(0, hptdir)
    
def module_check() :
    # TODO add module check !
    # FIXME : extract this into hydroplatform.__init__
    test_modules = ['numpy', 'sqlalchemy', 'networkx', 'shapely']
    for mod in test_modules:
        try:
            __import__(mod)
        except ImportError:
            raise 'Missing module %s. Please install it in order to make the HydroPlatform run' % mod


def set_thuban_path():
    # this could be done with thubaninit \
    # but it's not available when installed on win32
    import Thuban
    thubandir = os.path.join(Thuban.__path__[0], '..')
    dir = os.path.join(thubandir, "Lib")
    if os.path.isdir(dir):
        sys.path.insert(0, dir)

def win32_specific():
    # AUTOLOAD external libraries
    if sys.platform == 'win32':
        pass
        #for lib in constants.LIBS_AUTOLOAD:
        #    sys.path.append(lib)

def load_thuban():
    try:
        import Thuban
        set_thuban_path()
    except ImportError:
        # can't find Thuban
        if sys.platform == 'win32':
            # try to add default install directory of thuban into the path
            defdir = 'c:\\Program Files\\Thuban'
            if os.path.exists(defdir):
                sys.path.insert(0, defdir)    
            import Thuban
            set_thuban_path()
        elif sys.platform == 'linux2':
            # test if Debian package is installed
            defdir = '/usr/lib/thuban'
            if os.path.exists(defdir):
                sys.path.insert(0, defdir)
            import Thuban
            set_thuban_path()
        else:
            print "No thuban support"
    
# Start Hydroplatform
if __name__ == "__main__":
    logging.info("Dependency check")
    load_thuban()
    win32_specific()
    module_check()
    from hydroplatform.extension import *
    import hydroplatform.ui.main
    logging.info("Starting UI")
    hydroplatform.ui.main.main()
